package com.crpc.runner;

import com.crpc.common.crpcEnum.CrpcExceptionEnums;
import com.crpc.common.exception.CrpcException;
import com.crpc.common.utils.zookeeper.ZookeeperNode;
import com.crpc.common.utils.zookeeper.ZookeeperUtils;
import com.crpc.config.CrpcProperties;
import com.crpc.config.RegisterProperties;
import com.crpc.core.CrpcBootStrap;
import com.crpc.core.compress.CompressorFactory;
import com.crpc.core.compress.CompressorWrapper;
import com.crpc.core.config.Configuration;
import com.crpc.core.discovery.RegistryConfig;
import com.crpc.core.loadBalance.LoadBalanceFactory;
import com.crpc.core.loadBalance.LoadBalanceWrapper;
import com.crpc.core.serialize.SerializerFactory;
import com.crpc.core.serialize.SerializerWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;

import static com.crpc.common.utils.zookeeper.ZookeeperUtils.createNode;

@Slf4j
@Component
public class RpcConfigPostConstruct {
    @Autowired
    RegisterProperties registerProperties;

    @Autowired
    CrpcProperties crpcProperties;

    @PostConstruct
    @Order(0)
    public void init() {
        String connectUrl = registerProperties.getHost() + ":" + registerProperties.getPort();
        try (ZooKeeper zooKeeper = ZookeeperUtils.createZooKeeper(connectUrl, 1000000000)) {
            //定义节点和数据
            String basePth = "/crpc-metadata";
            String providerPath = basePth + "/providers";
            String consumerPath = basePth + "/consumers";
            ZookeeperNode build1 = ZookeeperNode.builder().nodePath(basePth).build();
            ZookeeperNode build2 = ZookeeperNode.builder().nodePath(providerPath).build();
            ZookeeperNode build3 = ZookeeperNode.builder().nodePath(consumerPath).build();
            ArrayList<ZookeeperNode> nodes = new ArrayList<>();
            nodes.add(build1);
            nodes.add(build2);
            nodes.add(build3);
            nodes.forEach(node -> createNode(node, zooKeeper, null, CreateMode.PERSISTENT));
            log.info("zookeeper 初始节点创建成功");
            log.info("开始初始化加载 xml 和 spi ");
            Configuration configuration = CrpcBootStrap.getInstance().getConfiguration();
            log.info("初始化的configuration =====>{}",configuration.toString());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("zookeeper 初始节点创建成功");
            throw new CrpcException(CrpcExceptionEnums.ZOOKEEPER_ERROR);
        }
    }

    @PostConstruct
    @Order(1)
    public com.crpc.core.config.Configuration configuration() {
        log.info("获取 springboot yml 文件属性，给 configuration 复制");
        com.crpc.core.config.Configuration configuration = CrpcBootStrap.getInstance().getConfiguration();
        SerializerWrapper serializerWrapper = SerializerFactory.getSerializer(crpcProperties.getSerializerType().getCode());
        configuration.setSerializer(serializerWrapper.getSerializer());
        CompressorWrapper compressorWrapper = CompressorFactory.getCompressor(crpcProperties.getCompressorType().getCode());
        configuration.setCompressor(compressorWrapper.getCompressor());
        LoadBalanceWrapper loadBalanceWrapper = LoadBalanceFactory.getLoadBalance(crpcProperties.getLoadBalancerType().getCode());
        configuration.setLoadBalance(loadBalanceWrapper.getLoadBalance());
        configuration.setPort(crpcProperties.getNettyPort());
        configuration.setApplicationName(crpcProperties.getCrpcName());
        configuration.setProviderHost(crpcProperties.getProviderHost());
        configuration.setConsumerHost(crpcProperties.getConsumerHost());
        //zookeeper://127.0.0.1:21810
        configuration.setRegistryConfig(new RegistryConfig(registerProperties.getRegistryType().getType() + "://" + registerProperties.getHost() + ":" + registerProperties.getPort()));
        log.info("springboot yml 文件属性赋值后的 configuration=====>{}", configuration);
        return configuration;
    }

    @PostConstruct
    @Order(3)
    public void test() {
        File directory = new File(".");
        System.out.println("=============================>" + directory.getAbsolutePath());
    }
}